<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>proxy实例的deleteProperty方法</title>
</head>
<body>
  <script>
    // deleteProperty方法用于拦截delete操作。如果这个方法抛出错误或者返回false,
    // 当前属性就无法被delete命令删除。
    var handler = {
      deleteProperty (target, key) {
        invariant(key, 'delete');
        delete target[key];
        return true;
      }
    };
    function invariant (key, action) {
      if (key[0] === '_') {
        throw new Error(`Invalid attempt to ${action} private "${key}" property`)
      }
    }
    var target = {_prop: 'foo'};
    var proxy = new Proxy(target, handler);
    // delete proxy._prop
    // Error: Invalid attempt to delete private "_prop" property
    /**
     * 上面代码中，deleteProperty方法拦截了delete操作符，删除第一个字符为下划线的属性会报错
     * 注意：目标对象自身的不可配置(configurable)的属性，不能被deleteProperty方法删除，否则会报错
    */
  </script>
</body>
</html>